import 'dart:convert';

import 'package:custompaint/bean/TabBean.dart';
import 'package:custompaint/bean/WidgetType.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class Util {
  static String getColorValue(Color color) => color == Colors.transparent
      ? '0xFFFFFFFF(透明)'
      : '0x${color.value.toRadixString(16).padLeft(6, '0').toUpperCase()}';

  static Color? string2Color(String inputColor) {
    try {
      int value = int.parse(inputColor.substring(2), radix: 16);
      double opacity = ((value >> 24) & 0xFF) / 255.0;
      int red = (value >> 16) & 0xFF;
      int green = (value >> 8) & 0xFF;
      int blue = value & 0xFF;
      return Color.fromRGBO(red, green, blue, opacity);
    } catch (error) {
      return null;
    }
  }

  static Future<String> genCode(List<TabBean> frames, int background,
      double panelHeight, double panelWidth) async {
    List<WidgetType> types = [];
    List<String> allType = [];
    for (var value in frames) {
      types.addAll(value.list.map((e) => e.type).toList());
    }
    allType.addAll(types.map((e) => e.name).toSet().toList());
    String txt = await rootBundle.loadString("assets/template.dart");
    txt = txt.replaceAll(
        "JSON_VALUE", jsonEncode(frames.map((e) => e.toJson()).toList()));
    txt = txt.replaceAll("BACKGROUND_VALUE", "$background");
    String SWITCH_VALUES = "";
    String DRAW_EVENT_VALUE = "";
    if (allType.contains('直线')) {
      SWITCH_VALUES = "$SWITCH_VALUES\n${"""case "直线":
          _drawLine(fakeWidget, paint, canvas, size);"""}";
      DRAW_EVENT_VALUE =
          '$DRAW_EVENT_VALUE\n${"""void _drawLine(OutputBean fakeWidget, Paint paint, Canvas canvas, Size size) {
    paint.color = Color(fakeWidget.color);
    paint.strokeWidth = fakeWidget.strokeWidth;
    paint.strokeCap = StrokeCap.values
        .firstWhere((element) => fakeWidget.strokeCap == element.name);
    canvas.drawLine(
      Offset(size.width * fakeWidget.startX, size.height * fakeWidget.startY),
      Offset(size.width * fakeWidget.endX, size.height * fakeWidget.endY),
      paint,
    );
  }"""}';
    }

    if (allType.contains('曲线')) {
      SWITCH_VALUES = "$SWITCH_VALUES\n${"""case "曲线":
          _drawCurve(fakeWidget, paint, canvas, size);"""}";
      DRAW_EVENT_VALUE = '$DRAW_EVENT_VALUE\n${"""void _drawCurve(
      OutputBean fakeWidget, Paint paint, Canvas canvas, Size size) {
    paint.color = Color(fakeWidget.color);
    paint.strokeWidth = 1;
    paint.style = PaintingStyle.values
        .firstWhere((element) => element.name == fakeWidget.style);
    paint.strokeWidth = fakeWidget.strokeWidth;
    final Path path = Path();
    path.moveTo(
        size.width * fakeWidget.startX, size.height * fakeWidget.startY);
    for (var i = 0; i < fakeWidget.curvePointList.length; i++) {
      var point = fakeWidget.curvePointList[i];
      path.quadraticBezierTo(
          size.width * point.startX,
          size.height * point.startY,
          size.width * point.endX,
          size.height * point.endY);
    }
    path.close();
    Rect bounds = path.getBounds();
    double centerX = bounds.center.dx;
    double centerY = bounds.center.dy;
    canvas.save();
    canvas.translate(centerX, centerY);
    canvas.rotate(fakeWidget.angle * pi / 180);
    canvas.translate(-centerX, -centerY);
    canvas.drawPath(path, paint);
    canvas.restore();
  }"""}';
    }
    if (allType.contains('圆形')) {
      SWITCH_VALUES = "$SWITCH_VALUES\n${"""case "圆形":
          _drawCircle(fakeWidget, paint, canvas, size);"""}";
      DRAW_EVENT_VALUE = '$DRAW_EVENT_VALUE\n${"""void _drawCircle(
      OutputBean fakeWidget, Paint paint, Canvas canvas, Size size) {
    paint.color = Color(fakeWidget.color);
    paint.strokeWidth = fakeWidget.strokeWidth;
    paint.style = PaintingStyle.values
        .firstWhere((element) => element.name == fakeWidget.style);
    canvas.drawCircle(
        Offset(size.width * fakeWidget.startX, size.height * fakeWidget.startY),
        fakeWidget.circleRadius,
        paint);
  }"""}';
    }
    if (allType.contains('椭圆')) {
      SWITCH_VALUES = "$SWITCH_VALUES\n${"""case "椭圆":
          _drawElliptic(fakeWidget, paint, canvas, size);"""}";
      DRAW_EVENT_VALUE = '$DRAW_EVENT_VALUE\n${"""void _drawElliptic(
      OutputBean fakeWidget, Paint paint, Canvas canvas, Size size) {
    paint.color = Color(fakeWidget.color);
    paint.strokeWidth = fakeWidget.strokeWidth;
    paint.style = PaintingStyle.values
        .firstWhere((element) => element.name == fakeWidget.style);
    double left = size.width * fakeWidget.rectLeft;
    double top = size.height * fakeWidget.rectTop;
    double right = size.width * fakeWidget.rectRight;
    double bottom = size.height * fakeWidget.rectBottom;
    double centerX = (left + right) / 2;
    double centerY = (top + bottom) / 2;
    canvas.save();
    canvas.translate(centerX, centerY);
    canvas.rotate(fakeWidget.angle * pi / 180);
    canvas.drawOval(
        Rect.fromCenter(
            center: Offset.zero, width: right - left, height: bottom - top),
        paint);
    canvas.restore();
  }"""}';
    }
    if (allType.contains('N边形')) {
      SWITCH_VALUES = "$SWITCH_VALUES\n${"""case "N边形":
          _drawNPath(fakeWidget, paint, canvas, size);"""}";
      DRAW_EVENT_VALUE = '$DRAW_EVENT_VALUE\n${"""void _drawNPath(
      OutputBean fakeWidget, Paint paint, Canvas canvas, Size size) {
    paint.color = Color(fakeWidget.color);
    paint.strokeWidth = 1;
    paint.style = PaintingStyle.values
        .firstWhere((element) => element.name == fakeWidget.style);
    paint.strokeWidth = fakeWidget.strokeWidth;
    final Path path = Path();
    path.moveTo(
        size.width * fakeWidget.startX, size.height * fakeWidget.startY);
    for (var i = 0; i < fakeWidget.curvePointList.length; i++) {
      var point = fakeWidget.curvePointList[i];
      path.lineTo(size.width * point.startX, size.height * point.startY);
    }
    path.close();
    Rect bounds = path.getBounds();
    double centerX = bounds.center.dx;
    double centerY = bounds.center.dy;
    canvas.save();
    canvas.translate(centerX, centerY);
    canvas.rotate(fakeWidget.angle * pi / 180);
    canvas.translate(-centerX, -centerY);
    canvas.drawPath(path, paint);
    canvas.restore();
  }"""}';
    }
    txt = txt.replaceAll("SWITCH_VALUE", SWITCH_VALUES);
    txt = txt.replaceAll("DRAW_EVENT_VALUE", DRAW_EVENT_VALUE);
    txt = txt.replaceAll("PANEL_WIDTH_VALUE", "$panelWidth");
    txt = txt.replaceAll("PANEL_HEIGHT_VALUE", "$panelHeight");
    return txt;
  }
}
